package main

import (
	"fmt"
	"math"
)
//panic抛异常	recover捕获异常
//recover仅在defer函数中使用才能捕获异常，此时函数的执行流程已经中断，无法恢复到后续位置继续执行
type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
	return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
}

func Sqrt(x float64) (float64, error) {
	if x < 0 {
		return 0, ErrNegativeSqrt(x)
	}
	return math.Sqrt(x), nil
}
func Test() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()
	divide(5, 0)
	fmt.Println("end of Test")
}
func divide(a, b int) int {
	return a / b
}
func main() {
	fmt.Println(Sqrt(2))
	fmt.Println(Sqrt(-2))
	Test()
}